/******************************************************
 * @file pic_operation.c
 * @author Destiny 
 * @brief 图片处理
 * 
 * @version 0.1
 * @date 2024-07-18
 *****************************************************/
#include <config.h>
#include <pic_operation.h>
#include <stdlib.h>
#include <string.h>

/**
 * @brief 图片缩放
 * 
 * @param ptOriginPic 原始图片
 * @param ptZoomPic 缩放后的图片
 * @return int 成功：0;失败：其他
*/
int PicZoom(PT_PixelDatas ptOriginPic, PT_PixelDatas ptZoomPic)
{
    unsigned long dwDstWidth = ptZoomPic->iWidth;
    unsigned long* pdwSrcXTable;
	unsigned long x;
	unsigned long y;
	unsigned long dwSrcY;
	uint8_t *pucDest;
	uint8_t *pucSrc;
	unsigned long dwPixelBytes = ptOriginPic->iBpp/8;
	if (ptOriginPic->iBpp != ptZoomPic->iBpp)
	{
		return -1;
	}
    pdwSrcXTable = malloc(sizeof(unsigned long) * dwDstWidth);
    if (NULL == pdwSrcXTable)
    {
        DBG_PRINTF(DLOG_LVL_ERROR,DLOG_TAG,"malloc error!\n");
        return -1;
    }
    for (x = 0; x < dwDstWidth; x++)
    {
        pdwSrcXTable[x]=(x*ptOriginPic->iWidth/ptZoomPic->iWidth);
    }
    for (y = 0; y < ptZoomPic->iHeight; y++)
    {			
        dwSrcY = (y * ptOriginPic->iHeight / ptZoomPic->iHeight);

		pucDest = ptZoomPic->aucPixelDatas + y*ptZoomPic->iLineBytes;
		pucSrc  = ptOriginPic->aucPixelDatas + dwSrcY*ptOriginPic->iLineBytes;
		
        for (x = 0; x <dwDstWidth; x++)
        {
			 memcpy(pucDest+x*dwPixelBytes, pucSrc+pdwSrcXTable[x]*dwPixelBytes, dwPixelBytes);
        }
    }
    free(pdwSrcXTable);
	return 0;
}

/**
 * @brief 两张图片融合
 * 
 * @param iX 融合起点（左上角坐标）
 * @param iY 
 * @param ptSmallPic 小图片
 * @param ptBigPic   大图片
 * @return int 	    成功：0;失败：其他
*/
int PicMerge(int iX, int iY, PT_PixelDatas ptSmallPic, PT_PixelDatas ptBigPic)
{
	int i;
	uint8_t *pucSrc;
	uint8_t *pucDst;
	
	if ((ptSmallPic->iWidth > ptBigPic->iWidth)  ||
		(ptSmallPic->iHeight > ptBigPic->iHeight) ||
		(ptSmallPic->iBpp != ptBigPic->iBpp))
	{
		return -1;
	}

	pucSrc = ptSmallPic->aucPixelDatas;
	pucDst = ptBigPic->aucPixelDatas + iY * ptBigPic->iLineBytes + iX * ptBigPic->iBpp / 8;
	for (i = 0; i < ptSmallPic->iHeight; i++)
	{
		memcpy(pucDst, pucSrc, ptSmallPic->iLineBytes);
		pucSrc += ptSmallPic->iLineBytes;
		pucDst += ptBigPic->iLineBytes;
	}
	return 0;
}

